<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 2017/1/6
 * Time: 19:53
 */

namespace app\src\buyorder\logic;


use app\src\base\logic\BaseLogic;
use app\src\buyorder\model\BuyOrder;
use app\src\user\logic\UserMemberLogic;
use app\src\wallet\logic\WalletLogic;
use app\weixin\controller\Income;
use app\src\extend\Page;
use think\Db;

class BuyOrderLogic extends BaseLogic{
    public function _init()
    {
        $this->setModel(new BuyOrder());
    }


    public function query_order($map = null, $page = ['curpage'=>1,'size'=>10], $order = false, $params = false){

        $model=$this->getModel();
        $fields='bo.*,p.name,pl.name as nickname,bo.goods_uid,cm.nickname as goods_name,um.mobile,mc.nickname as username,mc.head';
        $start = max(intval($page['curpage'])-1,0) * intval($page['size']);
        $query =$model->alias("bo")
            ->field($fields)
            ->join(["itboye_product"=>"p"],"p.id = bo.pid","left")
            ->join(['common_member'=>'mc'],'mc.uid = bo.uid','left')
            ->join(['itboye_platform'=>'pl'],'pl.uid = bo.goods_uid','left')
            ->join(['common_member'=>'cm'],'cm.uid = bo.goods_uid','left')
            ->join(['itboye_ucenter_member'=>'um'],'um.id = bo.uid','left')
            ->join(['itboye_user_relation'=>'ur'],'ur.lower_uid =bo.uid','left')
            ->where($map)
            ->order($order)
            ->where($map);


        $list = $query
            -> limit($start,$page['size'])
            -> select();

        $count = $model->alias("bo")
            ->field($fields)
            ->join(["itboye_product"=>"p"],"p.id = bo.pid","left")
            ->join(['common_member'=>'mc'],'mc.uid = bo.uid','left')
            ->join(['itboye_platform'=>'pl'],'pl.uid = bo.goods_uid','left')
            ->join(['common_member'=>'cm'],'cm.uid = bo.goods_uid','left')
            ->join(['itboye_ucenter_member'=>'um'],'um.id = bo.uid','left')
            ->join(['itboye_user_relation'=>'ur'],'ur.lower_uid =bo.uid')
            ->where($map)
            ->order($order)
            ->where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();
        $data = [];
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }else{
                array_push($data,$vo);
            }
        }
        return $this -> apiReturnSuc(["show" => $show, "list" => $data]);

    }
}